package cn.daniellee.plugin.pm.menu;

import cn.daniellee.plugin.pm.PartnerMission;
import cn.daniellee.plugin.pm.component.ItemGenerator;
import cn.daniellee.plugin.pm.core.MissionCore;
import cn.daniellee.plugin.pm.model.Mission;
import cn.daniellee.plugin.pm.model.PlayerData;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MissionMenu {

	public static Inventory generate(Player player, boolean byTimes, boolean offline, int page) {
		List<PlayerData> players = MissionCore.getAndSortFilterPlayers(player.getName(), byTimes, offline);
		Inventory menu = Bukkit.createInventory(new MissionMenuHolder(), 54, PartnerMission.getInstance().getConfig().getString("menu.mission.title", "&6&lPartner Mission").replace("&", "§"));

		int[] corners = { 0, 4, 8, 18, 22, 26 };
		ItemStack corner = MenuCommon.getCorner();
		Arrays.stream(corners).forEach(item -> menu.setItem(item, corner));
		int[] borders = { 1, 2, 3, 5, 6, 7, 9, 13, 17, 19, 20, 21, 23, 24, 25};
		ItemStack border = MenuCommon.getBorder();
		Arrays.stream(borders).forEach(item -> menu.setItem(item, border));

		PlayerData selfData = PartnerMission.getInstance().getStorage().getPlayerByName(player.getName());
		menu.setItem(11, ItemGenerator.getSkullItem(selfData.getName(), selfData.getTexture(), PartnerMission.getInstance().getConfig().getString("menu.mission.button.self.name", "&b&l{player}").replace("{player}", player.getName()), MissionCore.formatPlayerLore(PartnerMission.getInstance().getConfig().getStringList("menu.mission.button.self.lore"), selfData)));

		Mission mission = MissionCore.getPlayerActiveMission(player.getName());
		if (mission == null) { // 没有激活中的任务
			menu.setItem(15, ItemGenerator.getItem(PartnerMission.getInstance().getConfig().getString("menu.mission.button.pending.name", "&a&lPending invitation: &b&l{number}").replace("{number}", Long.toString(MissionCore.invites.stream().filter(item -> item.getInvitee().equals(player.getName())).count())), PartnerMission.getInstance().getConfig().getStringList("menu.mission.button.pending.lore").stream().map(item -> item.replace("{invitation}", Integer.toString(PartnerMission.getInstance().getConfig().getInt("setting.invitation-period", 3))).replace("{invitation}", Integer.toString(PartnerMission.getInstance().getConfig().getInt("setting.invitation-period", 3))).replace("{execution}", Integer.toString(PartnerMission.getInstance().getConfig().getInt("setting.execution-period", 30))).replace("{cooldown}", Integer.toString(PartnerMission.getInstance().getConfig().getInt("setting.cooldown-period", 60)))).collect(Collectors.toList()), PartnerMission.getInstance().getConfig().getString("menu.mission.button.pending.item.material", "ENDER_CHEST"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.button.pending.item.durability", 0)));

			String playerName = PartnerMission.getInstance().getConfig().getString("menu.mission.button.player.name", "&a&lPlayer: &b&l{player}");
			List<String> playerLore = PartnerMission.getInstance().getConfig().getStringList("menu.mission.button.player.lore");
			for (int i = 0; i < 18; i++) {
				int targetIndex = i + 18 * (page - 1);
				if (targetIndex >= players.size()) break;
				PlayerData playerData = players.get(targetIndex);
				menu.setItem(i + 27, ItemGenerator.getSkullItem(playerData.getName(), playerData.getTexture(), playerName.replace("{player}", playerData.getName()), MissionCore.formatPlayerLore(playerLore, playerData)));
			}

			menu.setItem(48, ItemGenerator.getItem(PartnerMission.getInstance().getConfig().getString("menu.mission.button.sort.name", "&3&lSort by: {sort}").replace("{sort}", byTimes ? PartnerMission.getInstance().getConfig().getString("menu.fields.sort.times", "&6&lComplete number") : PartnerMission.getInstance().getConfig().getString("menu.fields.sort.online", "&d&lOnline time")), PartnerMission.getInstance().getConfig().getStringList("menu.mission.button.sort.lore"), PartnerMission.getInstance().getConfig().getString("menu.mission.button.sort.item.material", "CHEST_MINECART"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.button.sort.item.durability", 0)));
			menu.setItem(50, ItemGenerator.getItem(PartnerMission.getInstance().getConfig().getString("menu.mission.button.status.name", "&3&lFilter: {status}").replace("{status}", offline ? PartnerMission.getInstance().getConfig().getString("menu.fields.status.offline", "&c&lOffline") : PartnerMission.getInstance().getConfig().getString("menu.fields.status.online", "&a&lOnline")), PartnerMission.getInstance().getConfig().getStringList("menu.mission.button.status.lore"), PartnerMission.getInstance().getConfig().getString("menu.mission.button.status.item.material", "HOPPER_MINECART"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.button.status.item.durability", 0)));

			// 上一页按钮
			if (page > 1) {
				menu.setItem(45, ItemGenerator.getItem(PartnerMission.getInstance().getConfig().getString("menu.common.back.name", "&eBack to {page}").replace("{page}", Integer.toString(page - 1)), null, PartnerMission.getInstance().getConfig().getString("menu.common.back.item.material", "RED_WOOL"), PartnerMission.getInstance().getConfig().getInt("menu.common.back.item.durability", 0)));
			}
			// 下一页按钮
			if (18 * page < players.size()) {
				menu.setItem(53, ItemGenerator.getItem(PartnerMission.getInstance().getConfig().getString("menu.common.next.name", "&eNext to {page}").replace("{page}", Integer.toString(page + 1)), null, PartnerMission.getInstance().getConfig().getString("menu.common.next.item.material", "LIME_WOOL"), PartnerMission.getInstance().getConfig().getInt("menu.common.next.item.durability", 0)));
			}
		} else {
			String partner = mission.getInviter().equals(player.getName()) ? mission.getInvitee() : mission.getInviter();
			PlayerData partnerData = PartnerMission.getInstance().getStorage().getPlayerByName(partner);
			menu.setItem(15, ItemGenerator.getSkullItem(partner, partnerData.getTexture(), PartnerMission.getInstance().getConfig().getString("menu.mission.button.partner.name", "&a&lPartner: &b&l{player}").replace("{player}", partner), MissionCore.formatPlayerLore(PartnerMission.getInstance().getConfig().getStringList("menu.mission.button.partner.lore"), partnerData)));

			List<String> fellingLore = new ArrayList<>(MissionCore.felling.get(mission.getFellingIndex()).getDescription());
			fellingLore.add(MissionCore.getMissionProgressLore(0, mission));
			menu.setItem(27, ItemGenerator.getItem(MissionCore.felling.get(mission.getFellingIndex()).getTitle(), fellingLore, PartnerMission.getInstance().getConfig().getString("menu.mission.items.felling.material", "DIAMOND_AXE"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.items.felling.durability", 0)));

			List<String> miningLore = new ArrayList<>(MissionCore.mining.get(mission.getMiningIndex()).getDescription());
			miningLore.add(MissionCore.getMissionProgressLore(1, mission));
			menu.setItem(29, ItemGenerator.getItem(MissionCore.mining.get(mission.getMiningIndex()).getTitle(), miningLore, PartnerMission.getInstance().getConfig().getString("menu.mission.items.mining.material", "DIAMOND_PICKAXE"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.items.mining.durability", 0)));

			List<String> monsterLore = new ArrayList<>(MissionCore.killMonster.get(mission.getKillMonsterIndex()).getDescription());
			monsterLore.add(MissionCore.getMissionProgressLore(2, mission));
			menu.setItem(31, ItemGenerator.getItem(MissionCore.killMonster.get(mission.getKillMonsterIndex()).getTitle(), monsterLore, PartnerMission.getInstance().getConfig().getString("menu.mission.items.monster.material", "TRIDENT"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.items.monster.durability", 0)));

			List<String> animalLore = new ArrayList<>(MissionCore.killAnimal.get(mission.getKillAnimalIndex()).getDescription());
			animalLore.add(MissionCore.getMissionProgressLore(3, mission));
			menu.setItem(33, ItemGenerator.getItem(MissionCore.killAnimal.get(mission.getKillAnimalIndex()).getTitle(), animalLore, PartnerMission.getInstance().getConfig().getString("menu.mission.items.animal.material", "DIAMOND_SWORD"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.items.animal.durability", 0)));

			List<String> cropLore = new ArrayList<>(MissionCore.gatherCrop.get(mission.getGatherCropIndex()).getDescription());
			cropLore.add(MissionCore.getMissionProgressLore(4, mission));
			menu.setItem(35, ItemGenerator.getItem(MissionCore.gatherCrop.get(mission.getGatherCropIndex()).getTitle(), cropLore, PartnerMission.getInstance().getConfig().getString("menu.mission.items.crop.material", "DIAMOND_HOE"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.items.crop.durability", 0)));

			List<String> rewardLore = new ArrayList<>(MissionCore.reward.get(mission.getRewardIndex()).getDescription());
			rewardLore.add(mission.getProgress() == 5 ? PartnerMission.getInstance().getConfig().getString("menu.fields.reward.available", "&6&lClick to receive (Attention backpack)") : PartnerMission.getInstance().getConfig().getString("menu.fields.reward.undone", "&d&lTime remaining: {remain} minutes").replace("{remain}", Long.toString((mission.getValidUntil() - System.currentTimeMillis()) / 1000 / 60)));
			menu.setItem(49, ItemGenerator.getItem(MissionCore.reward.get(mission.getRewardIndex()).getTitle(), rewardLore, PartnerMission.getInstance().getConfig().getString("menu.mission.items.reward.material", "CHEST"),  PartnerMission.getInstance().getConfig().getInt("menu.mission.items.reward.durability", 0)));
		}
		return menu;
	}

	public static class MissionMenuHolder implements InventoryHolder {
		@Override
		public Inventory getInventory() {
			return Bukkit.createInventory(null, 54);
		}
	}
}
